<?php

namespace App\Models\GameServer;

use Illuminate\Database\Eloquent\Model;

class UserInfo extends Model
{
    protected $table = 'user_info';
    protected $primaryKey = 'id';
    protected $connection = 'game_server';
    protected $appends = [
        'channel'
    ];

    public $timestamps = false;

    protected $channel = [
        0 => '官方渠道',
        1 => 'IOS',
        2 => '应用宝',
        3 => '百度应用中心',
        4 => '小米',
        5 => '安智市场',
        6 => '360',
        7 => '机锋市场',
        8 => 'OPPO',
        9 => '腾讯',
        9 => '掌上应用汇',
        10 => '魅族应用商店',
        11 => '乐商店',
        12 => '优亿市场',
        13 => '酷安',
        14 => '木蚂蚁',
        15 => '非凡软件',
        16 => '沃商店',
        17 => '豌豆荚',
        18 => '淘宝应用中心',
        19 => '乐视',
        20 => '搜狗应用市场',
        21 => '华为',
        22 => 'N多市场',
        23 => '易用汇',
        24 => '三星应用商店',
        25 => '锤子',
        26 => '安卓网(pc6)',
        27 => '冒泡堂',
        28 => '历趣',
        29 => '多特下载',
        30 => '移动MM',
        31 => '当乐安致',
        32 => '酷派',
        33 => '泡椒',
        34 => '中兴汇天地',
        35 => '西西软件园'
    ];

    public function getChannelAttribute()
    {
        $channel = $this->attributes['distributer_id'];
        return $this->channel[$channel] ?? '其他';
    }


    public function getLoginTimeLatelyAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getRegTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    //计算胜率
    public function getWinRateAttribute()
    {
        $loss_times = $this->attributes['loss_times'];
        $win_times = $this->attributes['win_times'];
        $tie_times = $this->attributes['tie_times'];
        $total_times = $loss_times + $win_times + $tie_times;

        $win_rate = $total_times > 0 ? round($win_times / $total_times, 4)*100 . '%' : 0;

        return $win_rate;
    }

    //计算vip等级
    public function getVipRankAttribute()
    {
        $score = $this->attributes['vip_score'];
        $max_level = 100;
        $level_score = 0;

        //--1^2+2^2+3^2+4^2+……+n^2=n(n+1)*(2n+1)/6
        for ($level = 1; $level < $max_level; $level++) {
            $level_score = $level * ($level + 1) * (2 * $level + 1);
            if ($score < $level_score) {
                return $level - 1;
            }
        }

        return $max_level;

    }

}
